﻿using BlazorApp2.Data;
using BlazorApp2.Shared;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using Microsoft.AspNetCore.Components;
using System.ComponentModel.Design;

namespace BlazorApp2.Pages
{

    public partial class ToPdf
    {
        protected override void OnInitialized()
        {


            XtraReport baseReport = new();
            baseReport = CreateXtraReport();

            if (baseReport != null)
            {
                MemoryStream ms = new();
                PdfExportOptions opts = new();
                baseReport.ExportToPdf(ms, opts);
                ms.Seek(0, SeekOrigin.Begin);
                byte[] report = ms.ToArray();
                string pdfBase64 = Convert.ToBase64String(report);

                // 将 Base64 字符串传递到前端
                PdfUrl = "data:application/pdf;base64," + pdfBase64;
            }
        }

        private XtraReport CreateXtraReport()
        {
            XtraReport report = new();
            try
            {

                var list = param1.Split(',', StringSplitOptions.None);
                if (list.Count() > 1 || !string.IsNullOrWhiteSpace(param2) && param2.ToLower().Equals("p"))
                {

                    foreach (var item in list)
                    {
                        var BaseReport = new Report1();
                        BaseReport.ca = item;
                        BaseReport.CreateDocument();



                        report.Pages.AddRange(BaseReport.Pages);


                    }
                }
                else
                {
                    var BaseReport = new Report1();
                    BaseReport.ca = param1;
                    BaseReport.CreateDocument();
                    report.Pages.AddRange(BaseReport.Pages);

                }

            }
            catch (Exception ex)
            {
                var errorReport = new Error_Report();
                errorReport.Message = ex.Message;
                errorReport.CreateDocument();
                report = errorReport;
                return report;
            }


            return report;
        }
    }
}
